package model;

import dao.DaoException;
import dao.DaoFactory;
import dao.TemplateDao;
import java.util.HashMap;

/**
 * 
 * Template model class
 * 
 * The template class represents a template table in the database
 * Template body consists of tokens which could be system level or user 
 * defined. Each template belongs a particular user who created it. 
 * 
 * @author Sukhjinder K. Narula (narula.sk@gmail.com)
 * @since June 2013
 * @version $Revision: 1.0 $
 */
public class Template {
	
	// Dao
	private TemplateDao dao;
	
	// Properties
	private int id;
	private int userId;
	private String title;
	private String body;
	

	/**
	 * Constructor to create the template object
	 * 
	
	
	
	 * @param userId int
	 * @param title String
	 * @param body String
	 */
	public Template(int userId, String title, String body) {
		this.id = -1;
		this.userId = userId;
		this.title = title;
		this.body = body;
		this.dao = DaoFactory.getTemplateDao();
	}
	
	/**
	 * Gets a template id
	
	 * @return id: numeric unique value of the template */
	public int getId() {
		return id;
	}

	/**
	 * Sets a template id
	
	 * @param id int
	 */
	public void setId(int id) {
		this.id = id;
	}

	/**
	 * Gets a user id
	
	 * @return userId: numeric unique value of the user id */
	public int getUserId() {
		return userId;
	}

	/**
	 * Sets the user id.
	
	 * @param userId int
	 */
	public void setUserId(int userId) {
		this.userId = userId;
	}
	
	/**
	 * Gets a template title
	
	 * @return title: string value of the title of the template */
	public String getTitle() {
		return title;
	}

	/**
	 * Sets a template title
	
	 * @param title String
	 */
	public void setTitle(String title) {
		this.title = title;
	}

	/**
	 * Gets a template body
	
	 * @return body: string value of the body of the template */
	public String getBody() {
		return body;
	}

	/**
	 * Sets a template body
	
	 * @param body String
	 */
	public void setBody(String body) {
		this.body = body;
	}

	/**
	 * Returns the hash map representation of properties
	
	 * @return HashMap<String,String>
	 */
	public HashMap<String, String> getTransferObject(){
		
		HashMap<String, String> toReturn = new HashMap<String, String>();
		toReturn.put("Id", String.valueOf(id));
		toReturn.put("UserId", String.valueOf(userId));
		toReturn.put("Title", title);
		toReturn.put("Body", body);
		return toReturn;		
	}
	
	/**
	 * Saves (or creates new) template into the database
	
	 * @throws ModelException */
	public void save() throws ModelException{
		
		if (id == -1){
			try {
				id = dao.create(getTransferObject());
			} catch (DaoException e) {
				throw new ModelException(e.getMessage());
			}
		} else {
			try {
				dao.update(getTransferObject());
			} catch (DaoException e) {
				throw new ModelException(e.getMessage());
			}
		}
	}
	
	/**
	 * Deletes the current template from the database
	
	 * @throws ModelException  */
	public void delete() throws ModelException {
		try {
			dao.delete(id);
		} catch (DaoException e) {
			throw new ModelException(e.getMessage());
		}
	}
	
}
